home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / HyperCuber Source / HyperCuber 2.0 Source.sit / HyperCuber 2.0 Source / CControlsDirector.cp < prev    next >
Text File  |  1994-05-03  |  14KB  |  445 lines

  1. //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2. //| CControlsDirector.cp
  3. //|
  4. //| This file contains the implementation of the controls window director.
  5. //|___________________________________________________________________________
  6.  
  7. #include "CControlsDirector.h"
  8. #include "CControlsWindow.h"
  9. #include "CGraphic.h"
  10. #include "CHyperCuberPane.h"
  11. #include "CHyperCuberPrefs.h"
  12. #include "CHyperScrollBar.h"
  13. #include "CScrollNumPane.h"
  14.  
  15. #include "HyperCuber Balloons.h"
  16. #include "HyperCuber Commands.h"
  17. #include "HyperCuber Messages.h"
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21.  
  22.  
  23.  
  24. //============================ Constants ============================\\
  25.  
  26. #define    Pi    3.14159265358979323846
  27.  
  28.  
  29. //================================ Globals =======================\\
  30.  
  31. Boolean fCreatingBars;                //  TRUE if we are creating the scroll bars.  This variable
  32.                                     //    is necessary so the program doesn't respond to the
  33.                                     //    SetValue in the initialization as though the user had
  34.                                     //    moved the bar.
  35.  
  36.  
  37. //================================ External Globals =======================\\
  38.  
  39. extern CDesktop            *gDesktop;
  40. extern CBartender        *gBartender;
  41. extern CHyperCuberPrefs    *gPrefs;
  42.  
  43. extern Boolean            drawing_disabled;
  44.  
  45.  
  46. //============================ Procedure Prototypes =======================\\
  47.  
  48. pascal void    scroll_action_proc(ControlHandle control, short part);
  49. void        scroll_thumb_proc(CScrollBar *bar, short delta);
  50.  
  51.  
  52.  
  53. //================================== Types ================================\\
  54.  
  55. typedef struct
  56.     {
  57.     CHyperScrollBar    *bar;
  58.     long            dimension;
  59.     long            angle_num;    
  60.     } RefStruct;
  61.  
  62.  
  63.  
  64. //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  65. //| CControlsDirector::IControlsDirector
  66. //|
  67. //| Purpose: Initialize the director
  68. //|
  69. //| Parameters: aSupervisor:   the supervisor
  70. //|             pane:          the pane containing the graphic
  71. //|             the_dimension: the dimension it controls
  72. //|______________________________________________________________
  73.  
  74. void CControlsDirector::IControlsDirector(CDirectorOwner *aSupervisor, CHyperCuberPane *pane,
  75.                                             long the_dimension)
  76. {
  77.  
  78. #define CONTROLS_WINDOW_ID 130
  79.  
  80.     CDirector::IDirector(aSupervisor);                    //  Initialize superclass
  81.  
  82.     graphic_pane = pane;                                //  Remember the pane the graphic's in
  83.     dimension = the_dimension;                            //  Remember the dimension to control
  84.  
  85.     angle_bars = new(CList);                            //  Create the angle bars list
  86.     angle_bars->IList();
  87.     
  88.     BuildWindow();                                        //  Build the controls window
  89.     
  90.     if (gPrefs->prefs.
  91.             controls_window_visible[the_dimension])
  92.         itsWindow->Select();                            //  Show the window, if it's visible
  93.     
  94. }    //==== CControlsDirector::IControlsDirector() ====\\
  95.  
  96.  
  97.  
  98. //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  99. //| CControlsDirector::BuildWindow
  100. //|
  101. //| Purpose: This builds the controls window and adds the controls
  102. //|
  103. //| Parameters: none
  104. //|___________________________________________________________________________
  105.  
  106. void CControlsDirector::BuildWindow(void)
  107. {
  108.  
  109.     itsWindow = new(CControlsWindow);
  110.     ((CControlsWindow *) itsWindow)->
  111.             IControlsWindow(CONTROLS_WINDOW_ID,
  112.                     TRUE, gDesktop, this, dimension);    //  Create the window
  113.     itsWindow->helpResID = WINDOW_HELP_RES;                //  Link in Balloon help
  114.  
  115.     short window_height = dimension*20 + 5;
  116.     Rect size_rect;                                        //  Limit size of window to size of
  117.     gDesktop->GetBounds(&size_rect);                    //  desktop and make sure it's always
  118.     size_rect.left = 200;                                //  large enough to use the scroll bars
  119.     size_rect.top = window_height;
  120.     size_rect.bottom = window_height;
  121.     itsWindow->SetSizeRect(&size_rect);
  122.  
  123.     ((CControlsWindow *) itsWindow)->PlaceWithVerify(
  124.             &gPrefs->prefs.
  125.                 controls_window_position[dimension]);    //  Place window in default position
  126.                                                         //   (but not offscreen)
  127.  
  128.     Rect window_rect;
  129.     ((CControlsWindow *) itsWindow)->
  130.                             GetRect(&window_rect);        //  Find the window position
  131.     Point float_loc; 
  132.     float_loc.h = window_rect.left;
  133.     float_loc.v = window_rect.top;
  134.     itsWindow->SetShowFloatLoc(float_loc);                //  Set floating location
  135.  
  136.     const char left_brace[2] = "[";
  137.     const char right_brace[2] = "]";
  138.     const char colon[2] = ":";
  139.     const char Pchar[2] = "P";
  140.     char dimension_string[10];
  141.     NumToString(dimension, 
  142.                 (unsigned char *) dimension_string);    //  Convert dimension to a string
  143.     PtoCstr((unsigned char *) dimension_string);
  144.     
  145.     char bar_title[30];
  146.     strcpy(bar_title, left_brace);                        //  Create the perspective bar title
  147.     strcat(bar_title, dimension_string);
  148.     strcat(bar_title, colon);
  149.     strcat(bar_title, Pchar);
  150.     strcat(bar_title, right_brace);
  151.     
  152.     Lock(TRUE);                                            //  Lock this down so we can take addresses
  153.                                                         //    of perspective scroll bar
  154.     
  155.  
  156.     fCreatingBars = TRUE;                                //  Remember that we're creating bars now
  157.     
  158.     long vert = 5;
  159.     SetupScrollBar(vert, bar_title, &perspective_bar,     //  Set up the perspective scroll bar
  160.                     0,
  161.                     0, 10, (dimension <= 4) ? 7 : 3,    
  162.                     TRUE);
  163.  
  164.     Lock(FALSE);                                        //  Unlock this
  165.  
  166.     long i;                                                //  Set up the angle scroll bars
  167.     for (i = 1; i <= dimension-1; i++)                    
  168.         {
  169.         vert += 20;                                        //  Go down to next scroll bar
  170.         
  171.         char angle_num_string[10];
  172.         NumToString(i,
  173.                     (unsigned char*) angle_num_string);    //  Convert angle number to a string
  174.         PtoCstr((unsigned char *) angle_num_string);
  175.         
  176.         strcpy(bar_title, left_brace);                    //  Create the angle bar title
  177.         strcat(bar_title, dimension_string);
  178.         strcat(bar_title, colon);
  179.         strcat(bar_title, angle_num_string);
  180.         strcat(bar_title, right_brace);
  181.  
  182.         short value = 0;
  183.         if (dimension == 3)
  184.             value = 150;
  185.         
  186.         CHyperScrollBar    *angle_bar;
  187.         SetupScrollBar(vert, bar_title, &angle_bar,     //  Set up the angle scroll bar
  188.                         i,
  189.                         0, 360, value,
  190.                         FALSE);
  191.         
  192.         angle_bars->Append(angle_bar);                    //  Add this bar to the list
  193.         
  194.         }
  195.  
  196.     fCreatingBars = FALSE;                                //  We're creating the bars anymore
  197.  
  198. }    //==== CControlsDirector::BuildWindow() ====\\
  199.  
  200.  
  201.  
  202. //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  203. //| CControlsDirector::SetupScrollBar
  204. //|
  205. //| Purpose: This procedure adds a scroll bar and its title to the controls pane
  206. //|
  207. //| Parameters: voffset:       the vertical offset of the scroll bar from the top
  208. //|             title:         the title to put next to the scroll bar
  209. //|             bar:           the scroll bar to create and initialize
  210. //|             angle_num:     angle number of the angle controlled by this bar
  211. //|             min,max,value: minimum, maximum, and starting value of scroll bar
  212. //|             fPerspective:  TRUE if this is a perspective bar
  213. //|_______________________________________________________________________________
  214.  
  215. void CControlsDirector::SetupScrollBar(short voffset, char *title, CHyperScrollBar **bar,
  216.                                         long angle_num,
  217.                                         short min, short max, short value,
  218.                                         Boolean fPerspective)
  219. {
  220.  
  221.     LongRect interior_rect;                                //  Get the width of the window
  222.     itsWindow->GetInterior(&interior_rect);
  223.     long window_width = interior_rect.right -
  224.                         interior_rect.left;
  225.  
  226.     CEditText *text = new(CEditText);
  227.     text->IEditText(itsWindow, this,                    //  Set up the scroll bar text
  228.                     47, 24, 0, voffset,
  229.                     sizFIXEDLEFT, sizFIXEDTOP, -1);
  230.     text->SetTextPtr(title, strlen(title));
  231.     text->SetAlignCmd(cmdAlignRight);
  232.     text->SetFontNumber(systemFont);
  233.     text->SetFontSize(12);
  234.     text->Specify(kNotEditable, kNotSelectable, kNotStylable);
  235.  
  236.     char num_string[10];
  237.     text = new(CEditText);                                //  Set up the lower bound text
  238.     sprintf(num_string, "%d", min);
  239.     text->IEditText(itsWindow, this, 30, 20,
  240.                     68, voffset + 2,
  241.                     sizFIXEDLEFT, sizFIXEDTOP, -1);
  242.     text->SetTextPtr(num_string, strlen(num_string));
  243.     text->SetAlignCmd(cmdAlignRight);
  244.     text->SetFontNumber(geneva);
  245.     text->SetFontSize(9);
  246.     text->Specify(kNotEditable, kNotSelectable, kNotStylable);
  247.     
  248.     text = new(CEditText);                                //  Set up the upper bound text
  249.     sprintf(num_string, "%d", max);
  250.     text->IEditText(itsWindow, this, 25, 15,
  251.                     window_width - 25, voffset + 2,
  252.                     sizFIXEDRIGHT, sizFIXEDTOP, -1);
  253.     text->SetTextPtr(num_string, strlen(num_string));
  254.     text->SetAlignCmd(cmdAlignLeft);
  255.     text->SetFontNumber(geneva);
  256.     text->SetFontSize(9);
  257.     text->Specify(kNotEditable, kNotSelectable, kNotStylable);
  258.     
  259.     *bar = new(CHyperScrollBar);
  260.     (*bar)->IHyperScrollBar(itsWindow, this,            //  Set up the scroll bar
  261.                         HORIZONTAL, window_width - 127,
  262.                         100, voffset, !fPerspective);
  263.     (*bar)->hSizing = sizELASTIC;
  264.     (*bar)->vSizing = sizFIXEDTOP;
  265.     (*bar)->SetMinValue(0);
  266.     (*bar)->SetMaxValue(max - min);
  267.     (*bar)->SetValue(value - min);
  268.     (*bar)->SetActionProc(scroll_action_proc);
  269.     (*bar)->SetThumbFunc(scroll_thumb_proc);
  270.     (*bar)->helpResIndex = fPerspective ?
  271.             kPerspectiveScrollBar : kAngleScrollBar;    //  Link to Balloon Help
  272.  
  273.     RefStruct **ref =
  274.             (RefStruct **) NewHandle(sizeof(RefStruct));//  Create a new references structure
  275.     
  276.     (*ref)->bar = *bar;                                    //  Remember the CHyperScrollBar
  277.     (*ref)->dimension = dimension;                        //  Remember the dimension of this bar
  278.     (*ref)->angle_num = angle_num;                        //  Remember the angle number of this bar
  279.     
  280.     SetCRefCon((*bar)->macControl, (long) ref);            //  Remember the CHyperScrollBar handle
  281.  
  282.     CScrollNumPane *num_pane = new(CScrollNumPane);        //  Create a number pane to show the 
  283.     num_pane->IScrollNumPane(itsWindow, this,            //    scroll bar's setting
  284.                                 28, 20,
  285.                                 50, voffset + 2,
  286.                                 sizFIXEDLEFT, sizFIXEDTOP,
  287.                                 -1, *bar, min);
  288.     num_pane->SetAlignCmd(cmdAlignCenter);
  289.     
  290. }    //==== CControlsDirector::SetupScrollBar() ====\\
  291.  
  292.  
  293.  
  294. //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  295. //| CControlsDirector::ShowWindow
  296. //|
  297. //| Purpose: Show one of the controls windows
  298. //|
  299. //| Parameters: dimension: dimension controlled by the window to show
  300. //|___________________________________________________________________________
  301.  
  302. void CControlsDirector::ShowWindow(void)
  303. {
  304.  
  305.     itsWindow->Show();
  306.  
  307. }    //==== CControlsDirector::ShowWindow() ====\\
  308.  
  309.  
  310.  
  311. //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  312. //| CControlsDirector::HideWindow
  313. //|
  314. //| Purpose: Hide one of the controls windows
  315. //|
  316. //| Parameters: dimension: dimension controlled by the window to hide
  317. //|___________________________________________________________________________
  318.  
  319. void CControlsDirector::HideWindow(void)
  320. {
  321.  
  322.     itsWindow->Hide();
  323.  
  324. }    //==== CControlsDirector::HideWindow() ====\\
  325.  
  326.  
  327.  
  328. //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  329. //| Procedure: scroll_action_proc
  330. //|
  331. //| Purpose: This procedure is called whenever a scroll bar is clicked anywhere
  332. //|          but on the thumb.
  333. //|
  334. //| Parameters: control: the scroll bar which was moved
  335. //|             part:    where the bar was clicked
  336. //|___________________________________________________________________________
  337.  
  338. pascal void    scroll_action_proc(ControlHandle control, short part)
  339. {
  340.  
  341.     static short increment;
  342.     
  343.     RefStruct **ref =
  344.                 (RefStruct **) GetCRefCon(control);    //  Get the reference structure
  345.     
  346.     CHyperScrollBar *bar = (*ref)->bar;                //  Get the CHyperScrollBar handle
  347.     
  348.     switch(part)                                    //  Find the distance moved
  349.         {
  350.         case inUpButton: increment = -1; break;
  351.         case inDownButton: increment = 1; break;
  352.         case inPageUp: increment = -10; break;
  353.         case inPageDown: increment = 10; break;
  354.         }
  355.  
  356.     short value = bar->GetValue();                    //  Change the scroll bar's value.  Note that
  357.     value += increment;                                //    this also sends a controlValueChanged
  358.     bar->SetValue(value);                            //    message to the controls window director.
  359.             
  360. }    //==== scroll_action_proc() ====\\
  361.  
  362.  
  363.  
  364. //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  365. //| Procedure: scroll_thumb_proc
  366. //|
  367. //| Purpose: This procedure is called whenever a scroll bar thumb is moved
  368. //|
  369. //| Parameters: bar:   the scroll bar which was moved
  370. //|             delta: amount the thumb was moved
  371. //|___________________________________________________________________________
  372.  
  373. void    scroll_thumb_proc(CScrollBar *bar, short delta)
  374. {
  375.  
  376.     short value = bar->GetValue();    //  Change the scroll bar's value.  Note that this also sends                    
  377.     bar->SetValue(value);            //  a controlValueChanged message to the graphics pane
  378.  
  379. }    //==== scroll_thumb_proc() ====\\
  380.  
  381.  
  382.  
  383. //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  384. //| CControlsDirector::OffsetScrollBar
  385. //|
  386. //| Purpose: Offset the value of a scroll bar by some increment
  387. //|
  388. //| Parameters: angle:  the angle corresponding to the scroll bar to offset
  389. //|             offset: how much to offset the scroll bar
  390. //|__________________________________________________________________
  391.  
  392. void CControlsDirector::OffsetScrollBar(long angle, long offset)
  393. {
  394.  
  395.     CHyperScrollBar *bar = (angle == 0) ? perspective_bar :
  396.                 (CHyperScrollBar *) angle_bars->NthItem(angle);    //  Get the scroll bar
  397.     
  398.     bar->SetValue(bar->GetValue() + offset);                    //  Offset the value
  399.  
  400. }    //==== CControlsDirector::OffsetScrollBar() ====\\
  401.  
  402.  
  403.  
  404. //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  405. //| CControlsDirector::ProviderChanged
  406. //|
  407. //| Purpose: This is called every time a scroll bar changes value
  408. //|
  409. //| Parameters: provider: the control which changed
  410. //|             reason:   the reason we were called (controlValueChanged)
  411. //|             info:     unused
  412. //|__________________________________________________________________
  413.  
  414. void CControlsDirector::ProviderChanged(CCollaborator *provider, long reason, void *info)
  415. {
  416.  
  417.     if ((reason == controlValueChanged) && (!fCreatingBars))
  418.         {
  419.         CHyperScrollBar *bar = (CHyperScrollBar *) provider;    //  Get the scroll bar handle
  420.     
  421.         RefStruct **ref;
  422.         ref = (RefStruct **) GetCRefCon(bar->macControl);        //  Get the reference structure
  423.  
  424.         long angle_num = (*ref)->angle_num;                        //  Find the angle number
  425.     
  426.         short bar_value = bar->GetValue();                        //  Get the angle from the bar
  427.  
  428.         if (angle_num == 0)
  429.             graphic_pane->graphic->
  430.                     ChangePerspective(dimension, bar_value);    //  Change the amount of perspect.
  431.         
  432.         else
  433.             graphic_pane->graphic->ChangeAngle(dimension,
  434.                                 angle_num, bar_value*Pi/180);    //  Change the angle
  435.         
  436.         if (!drawing_disabled)    
  437.             graphic_pane->Draw((Rect *) NULL);                    //  Redraw the graphic
  438.         
  439.         }
  440.         
  441. }    //==== CControlsDirector::ProviderChanged() ====\\
  442.  
  443.  
  444.  
  445.